// 父组件获取子组件
// 2、引入ViewChild
import { Component, OnInit,ViewChild } from '@angular/core';

// 在js里进行路由跳转，需要先引入Router
// 为了能跳转并进行 传值 ，需要在这里引入 NavigationExtras
import{ Router,NavigationExtras} from '@angular/router'

@Component({
  selector: 'app-news',
  templateUrl: './news.component.html',
  styleUrls: ['./news.component.css']
})
export class NewsComponent implements OnInit {
  
  @ViewChild('footer',{static:true}) footer :any;

  // 定义普通数据
  // 在这里定义数据
  title="我是一个新闻组件11"
  // 推荐
  public msg:string="helloWord";

  // any是任意类型
  public msg2:any=122222+"saqqwwww";

  // object是对象类型
  public userInfo:object={
    username:"张三",
    age:20
  }

  // 这里先不赋值,只定义
  public message:any;

  // 这里是html
  public content:string="<h1>这个是Html H1</h1>"


  // 定义数组
  public arr=["111","222","333"]
  // 推荐 
  public list2:any[]=["222","新闻11111","122222"]

  public list3:Array<string>=["222321111112","21111111","122222"]

  public list4:any[]=[
    {name:"张三",age:23},
    {name:"张2",age:24},
    {name:"张5",age:25},
    {name:"张7",age:27}]

  public cars:any[]=[
    { cate:"宝马",list5:[{title:"宝马x1"},{title:"宝马x2"},{title:"宝马x3"}],price:"30万"},
    { cate:"奥迪",list5:[{title:"奥迪x1"},{title:"奥迪x2"},{title:"奥迪x3"}],price:"40万"},
  ]
  
  constructor(public router:Router) {
    
    this.message="这里的值改变了" 
    // 获取属性的值
    // console.log(this.message)

    // 改变属性的值
    this.message="又改变了"

   }

  ngOnInit() {
  }

  ngOnDestroy(){
    console.log("组件news的ngOnDestroy执行了")
  }

  getChildMsg(){
    // 获取子组件的msg
    alert(this.footer.msg) 
  }
  getChildMethod(){
    // 获取子组件里面的方法
    this.footer.run()
  }
  

  hello(e:any){
    console.log("子组件传递过来数据是："+e) // 子组件给父组件广播的数据
    alert("我是父组件的hello方法")
  }

  goNewsContent()
  {
    // 路由跳转，适合普通路由和动态路由
    // alert("hello")
    this.router.navigate(['/newscontent/','123'])
  }
  goShouye()
  {
    this.router.navigate(['/home/'])
  }

  goNews()
  {
    // 跳转并进行get传值
    let queryParams:NavigationExtras={
      queryParams:{'aid':123}
    }
    this.router.navigate(['/news/'],queryParams)
  }
}
